package com.nanbei.orderService.controller;


import com.nanbei.commonUntil.R;
import com.nanbei.orderService.service.PayLogService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-14
 */
@RestController
@RequestMapping("/orderService/payLog")
@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;


    /**
     * 生成微信支付二维码
     * @param orderNo
     * @return
     */
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo){
        Map map = payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    /**
     * 根据订单ID查询订单状态
     * @param orderNo
     * @return
     */
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo){
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println(map);

        if (map == null){
            return R.error().message("支付出错");
        }

        // trade_state 固定字段来判断状态
        if (map.get("trade_state").equals("SUCCESS")){
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }

}

